<?php 
  require(dirname(__FILE__).'/global.php');
  require(ROOT.'/admin/isLogin.php');     
  require(ROOT.'/include/xml.class.php'); 
  
  $submit = $_POST['submit'];
  $xmlPath = ROOT.'/common.xml';                                             
  $xml = new SimpleXMLUtil($xmlPath);
  if(isset($submit))
  {    
      $action = $_POST['action'];
      if($action == 'update')
      {
          $smtpServer = $_POST['smtpServer'];
          $smtpPort = $_POST['smtpPort'];
          $smtpApprove = $_POST['smtpApprove'];
          $smtpApprove = empty($smtpApprove) ? 'y' : $smtpApprove;
          $smtpFrom = $_POST['smtpFrom'];
          $smtpUser = $_POST['smtpUser'];
          $smtpPass = $_POST['smtpPass']; 
                                             
          $xml->setElementValue('SmtpServer',$smtpServer);
          $xml->setElementValue('SmtpPort',$smtpPort);
          $xml->setElementValue('SmtpApprove',$smtpApprove);
          $xml->setElementValue('SmtpFrom',$smtpFrom);
          $xml->setElementValue('SmtpUser',$smtpUser);
          $xml->setElementValue('SmtpPass',$smtpPass);  
          $xml->save();
          
          $smarty->assign('tip','更新成功');
      }
      else if($action == 'send')
      {
          require(ROOT.'/include/email.class.php');
          $to = $_POST['smtpTo'];
          if(empty($to))
          {                                          
              $smarty->assign('tip2','收件人地址不能为空');
          }
          else if(!DataConverter::isEmail($to))
          {                                          
              $smarty->assign('tip2','收件人地址格式不正确');
          }
          else
          {                                              
              $xml = new SimpleXMLUtil($xmlPath);
              $smtpServer = $xml->getElementValue("SmtpServer");  
              $smtpPort = $xml->getElementValue("SmtpPort");
              $smtpApprove = $xml->getElementValue("SmtpApprove");
              $smtpFrom = $xml->getElementValue("SmtpFrom");
              $smtpUser = $xml->getElementValue("SmtpUser");
              $smtpPass = $xml->getElementValue("SmtpPass");
              
              if($smtpApprove == 'y'){
                  //这里面的一个true是表示使用身份验证,否则不使用身份验证.
                  $smtp = new Smtp($smtpServer,$smtpPort,true,$smtpUser,$smtpPass);
              }
              else{                                                                       
                  $smtp = new Smtp($smtpServer,$smtpPort,false,$smtpUser,$smtpPass);
              }
              $smtp->debug = false;//是否显示发送的调试信息 
              if($smtp->sendmail($to, $smtpFrom, '121212', 'asdfasdf', 'HTML'))
              {
                  $smarty->assign('tip2','发送成功');
              }
              else
              {
                  $smarty->assign('tip2','发送失败');
              }
          }
      }
  }
  $smtpServer = $xml->getElementValue("SmtpServer");  
  $smtpPort = $xml->getElementValue("SmtpPort");
  $smtpApprove = $xml->getElementValue("SmtpApprove");
  $smtpFrom = $xml->getElementValue("SmtpFrom");
  $smtpUser = $xml->getElementValue("SmtpUser");
  $smtpPass = $xml->getElementValue("SmtpPass");      
  
  $smarty->assign('smtpServer',$smtpServer); 
  $smarty->assign('smtpPort',$smtpPort);
  $smarty->assign('smtpApprove',$smtpApprove);
  $smarty->assign('smtpFrom',$smtpFrom);
  $smarty->assign('smtpUser',$smtpUser);
  $smarty->assign('smtpPass',$smtpPass);
  $smarty->display('mail.html');
  $db->close();
?>
